import modelExtend from 'dva-model-extend'
import {message} from 'antd'
import { querySchool, addSchool, schoolDelete, schoolUpdate, addDepart, deleteDepart, 
    updateDepart, queryDepartmentList, querySpecialty, addSpecialty, deleteSpecialty,updateSpecialty, } from 'services/schoolManage'
import { pageModel } from './common'

export default modelExtend(pageModel, {
    namespace: 'schoolManage',

    state: {
        
    },

    subscriptions: {
        setup({ dispatch, history }) {
            history.listen((location) => {
                console.log(location)
                if (location.pathname.includes('/chzu/schoolManage/school')) {
                    dispatch({
                        type:'querySchool'
                    })
                }
                else if (location.pathname.includes('/chzu/schoolManage/department')){
                    const id = location.pathname.split('/')[location.pathname.split('/').length - 2];
                    dispatch({
                        type:'queryDepartmentList',
                        payload:id
                    })
                } else if (location.pathname.includes('/chzu/schoolManage/specialty')){
                    const id = location.pathname.split('/')[location.pathname.split('/').length - 3];
                    dispatch({
                        type:'querySpecialty',
                        payload: id
                    })
                }
            })
        },
    },

    effects: {
        *querySchool({payload},{call,put}){
            const data = yield call(querySchool);
            console.log(data)
            if(data.code === '200'){
                const schoolList = data.data;
                yield put({
                    type:'updateState',
                    payload:schoolList
                })
            }
        },
        *addSchool({payload},{call,put}){
            const data = yield call(addSchool,payload);
            console.log(data)
            if(data.code === '200'){
                yield put({
                    type:'querySchool'
                });
                message.success('新增学校成功！',3);
            }else{
                message.error(data.msg)
            }
        },
        *schoolDelete({payload},{call,put}){
            const data = yield call(schoolDelete,payload);
            if(data.code === '200'){
                yield put({
                    type:'querySchool',
                })
                message.success('删除成功！',3);
            }else{
                message.error(data.msg);
            }
        },
        *schoolUpdate({payload},{call,put}){
            const data = yield call(schoolUpdate,payload);
            console.log(data);
            if(data.code === '200'){
                yield put({
                    type:'querySchool'
                });
                message.success('修改成功！');
            }else{
                message.error(data.msg)
            }
        },
        *queryDepartmentList({payload},{call,put}){
            const data = yield call(queryDepartmentList,payload);
            console.log(data);
            if(data.code === '200'){
                const dataScourse = data.data;
                yield put({
                    type:'updateDepartment',
                    payload: dataScourse
                })
            }
        },
        *addDepart({payload},{call,put}){
            const data = yield call(addDepart,payload);
            if(data.code === '200'){
                yield put({
                    type:'queryDepartmentList',
                    payload: payload.schoolguid,
                })
                message.success('新增院系成功！',3);
            }else{
                message.error(data.msg);
            }
        },
        *deleteDepart({payload},{call,put}){
            const data = yield call(deleteDepart, payload.rowguid);
            if(data.code === '200'){
                yield put({
                    type:'queryDepartmentList',
                    payload:payload.schoolId
                })
                message.success('删除院系成功！',3);
            }else{
                message.error(data.msg);
            }
        },
        *updateDepart({payload},{call,put}){
            const data = yield call(updateDepart,payload);
            if(data.code === '200'){
                yield put({
                    type:'queryDepartmentList',
                    payload:payload.schoolId
                })
                message.success('修改院系名称成功！',3);
            }else{
                message.error(data.msg);
            }
        },
        *querySpecialty({payload},{call,put}){
            const data = yield call(querySpecialty,payload);
            if(data.code === '200'){
                const dataList = data.data;
                yield put({
                    type:'updateSpecialtyList',
                    payload:dataList
                })
            }else{
                message.error(data.msg)
            }
        },
        *addSpecialty({payload},{call,put}){
            const data = yield call(addSpecialty,payload);
            if(data.code === '200'){
                yield put({
                    type:'querySpecialty',
                    payload: payload.departmentguid
                })
                message.success('新增专业成功！',3);
            }else{
                message.error(data.msg);
            }
        },
        *deleteSpecialty({payload},{call,put}){
            const data = yield call(deleteSpecialty, payload.rowguid);
            if(data.code === '200'){
                yield put({
                    type: 'querySpecialty',
                    payload: payload.departmentguid
                })
                message.success('删除专业成功！',3);
            }else{
                message.error(data.msg)
            }
        },
        *updateSpecialty({payload},{call,put}){
            const data = yield call(updateSpecialty,payload);
            if(data.code === '200'){
                yield put({
                    type:'querySpecialty',
                    payload: payload.departmentguid
                });
                message.success('修改专业名称成功！',3);
            }else{
                message.error(data.msg)
            }
        }

       

    },

    reducers: {
        updateState(state,{payload}){
            return{
                ...state,
                schoolList:payload
            }
        },
        updateDepartment(state,{payload}){
            return{
                ...state,
                dataScourse:payload,
            }
        },
        updateSpecialtyList(state,{payload}){
            return{
                ...state,
                dataList: payload
            }
        }
        
    },
})
